<?php
##############################################
#
#   预约列表
# @ Time : 2017-1-8 - 下午9:20:27
# @ Author : JiangYongWei <1085562810@qq.com>
# @ Encoding : UTF-8 language : PHP
#
##############################################


namespace Admin\Model;

class MakeModel extends AdminBaseModel{
	/*预约表  */
	protected $tableName = 'make';	
	
	public function __construct($tableName=''){
		//初始化数据库对象
		if(!empty($tableName)){
			$this -> tableName = $tableName;
		}	
		parent::__construct($this -> tableName);
	}
	
	/**
	 * 获取预约列表
	 */
	public function getMake($name, $mobile, $type, $start_time, $end_time){
	 	if($name){
                    $map['name'] = array('like','%'.$name.'%');
		}
		if($mobile){
                    $map['mobile'] = array('like','%'.$mobile.'%');
		} 
		if($type){
		    $map['type'] = $type;
		}
                if($start_time && $end_time){
		    $map['m.add_time'] = array(array('egt', $start_time),array('elt', $end_time)) ;
		}
	    $count = $this->alias('m')->join('__APPLY__ as a on a.user_id=m.user_id')->field('m.*,a.type') -> where($map)->count();// 查询满足要求的总记录数
	    $Page = new \Think\Page($count, C('listRows'));
	    $show = $Page->show();
	    $result = $this
                    ->alias('m')
                    ->join('__APPLY__ as a on a.user_id=m.user_id')
                    ->field('m.*,a.type')
                    ->where($map)
                    ->order('m.id desc')
                    ->limit($Page->firstRow.','.$Page->listRows)
                    ->select();
            foreach ($result as $k=>$v){
                $apply_category = M('apply_category')->where('id='.$result[$k]['type'])->field('name')->find();
                $result[$k]['type_name'] = $apply_category['name'];
            }
		return array($result, $show);
	}
	
	/**
	 * 获取一个预约
	 */
	public function getMakeFind($id){
	    return $this->alias('m')->join('__APPLY__ as a on a.user_id=m.user_id')->field('m.*,a.type')->where("m.id = $id")->find();
	}
	
	/**
	 * 修改订单
	 */
	public function editActivity($id, $data){
	    return $this->where("id = $id")->save($data);
	    
	}
	
	/**
	* 删除预约
	*/
	public function deleteMake($id){
		return $this->where("id = $id")->delete();
	}
    
	/**
	 * 全选删除预约
	 */
	public function deleteAllMake($idArr) {
	    foreach ($idArr['make_id'] as $k => $v){
	        $this->where("id = $v")->delete();
	    }
	    return true;
	}
	
	
}